#include "xmlhelper.h"


void printPathInfo(PathInfo& rPathInfo)
{
	cout<<"TagName:"<<rPathInfo.sTagName;
	cout<<"Count:"<<rPathInfo.count<<endl;
}

void printPathVector(vector<PathInfo>& vPaths)
{
	for(auto it = vPaths.begin(); it != vPaths.end(); ++ it)
	{
		printPathInfo(*it);
	}
}

void printLotteryInfo(LotteryInfo& rLotteryInfo)
{
	cout<<"Name:"<<rLotteryInfo.sName<<endl;
	cout<<"StartTime:"<<rLotteryInfo.sStartTime<<endl;
	cout<<"EndTime:"<<rLotteryInfo.sEndTime<<endl;
	cout<<"Interval:"<<rLotteryInfo.iInterval<<endl;
	cout<<"XPath:";
	printPathVector(rLotteryInfo.vPaths);
	cout<<endl;
}

void printLotterys(vector<LotteryInfo>& rlotteries)
{
	for(auto it = rlotteries.begin(); it != rlotteries.end(); ++it)
	{
		printLotteryInfo(*it);
	}
}

vector<PathInfo> parseXpathInfo(TiXmlElement* pXpathRoot)
{
	vector<PathInfo> xPathVector;
	TiXmlNode* pPathNode = NULL;
	while(NULL != (pPathNode = pXpathRoot->IterateChildren(pPathNode)))
	{
		PathInfo pathInfo;

		int count = 0;
		if(TIXML_SUCCESS != pPathNode->ToElement()->QueryIntAttribute("count", &count))
		{
			//cerr<<"warn: count info is missing, using default 0."<<endl;
		}

		pathInfo.count = count;
		pathInfo.sTagName = pPathNode->ToElement()->Attribute("tag");

		xPathVector.push_back(pathInfo);
	}
	return xPathVector;
}

vector<LotteryInfo> parseConfigFile(string sConfigFile)
{
	TiXmlDocument doc(sConfigFile);
	bool isOpened = doc.LoadFile();
	if(!isOpened)
	{
		cerr<<"failed to open xml document"<<endl;
		exit(-1);
	}

	TiXmlElement* rootElement = doc.RootElement();
	TiXmlElement* lotteriesElement = rootElement->FirstChildElement("lotteries");

	TiXmlElement* pXpathElement = rootElement->FirstChildElement("xpath");
	vector<PathInfo> defaultXPathVector;
	if(!pXpathElement)
	{
		cerr<<"Default xpath not defined!!!"<<endl;
	}
	else
	{
		defaultXPathVector = parseXpathInfo(pXpathElement);
	}

	TiXmlNode* pLotteryNode = NULL;
	int nodeCount = 0;
	vector<LotteryInfo> lotteries;
	while(NULL != ( pLotteryNode = lotteriesElement->IterateChildren(pLotteryNode)))
	{
		LotteryInfo lottery;
		lottery.vPaths = defaultXPathVector;
		lottery.sName = pLotteryNode->ToElement()->Attribute("name");
		TiXmlNode* pNode = NULL;
		while(NULL != (pNode = pLotteryNode->IterateChildren(pNode)))
		{
			const char* pChar = pNode->ToElement()->Value();
			if( 0 == strcmp(pChar, "xpath"))
			{
				lottery.vPaths = parseXpathInfo(pNode->ToElement());
			}
			else if(0 == strcmp(pChar, "shedule"))
			{

				int interval = 100;
				if(TIXML_SUCCESS != pNode->ToElement()->QueryIntAttribute("interval", &interval))
				{
					//cerr<<"query interval failed, use 100 instead"<<endl;
				}

				lottery.iInterval = interval;
				lottery.sStartTime = pNode->ToElement()->Attribute("start");
				lottery.sEndTime = pNode->ToElement()->Attribute("end");
			}
			else
			{
				cout<<"other node"<<endl;
			}
		}
		lotteries.push_back(lottery);
	}
	return lotteries;
}

vector<string> parseLotteryResult(string sXmlString, LotteryInfo& rLotteryInfo)
{
	TiXmlDocument doc;
	doc.Parse(sXmlString.c_str());
	vector<string> result;
	auto sit = rLotteryInfo.vPaths.begin();
	TiXmlElement* pRoot = doc.FirstChildElement(sit->sTagName);
	++sit;

	for(; sit != rLotteryInfo.vPaths.end(); ++sit)
	{
		pRoot = pRoot->FirstChildElement(sit->sTagName);
		while(sit->count--)
		{
			pRoot = pRoot->NextSiblingElement();
		}
	}
	TiXmlElement* pTarget = pRoot;
	int LOTTERY_PER_PAGE = 40;
	while (LOTTERY_PER_PAGE--)
	{
		TiXmlElement* pLotteryNumber = NULL;
		TiXmlElement* pLotteryRight = NULL;
		pLotteryNumber = pTarget->FirstChildElement("td")->NextSiblingElement();
		string sLotteryNumber = pLotteryNumber->GetText();
		pLotteryRight = pLotteryNumber->NextSiblingElement()->FirstChildElement("span");
		string sLotteryRight = pLotteryRight->GetText();

		pTarget = pTarget->NextSiblingElement();
		result.push_back(sLotteryNumber + " " + sLotteryRight);
	}

	return result;
}